#!/usr/bin/python

"""
This module is responsible for everything GUI: create windows from glade files,
connect signals, etc.
"""

try:
    import pygtk
    pygtk.require("2.0")
    import gtk
except(Exception):
    print "Error importing modules. Try updating GTK."

# Declare all glade files
main_window_glade_file = "../glade/MainWindow.xml"
about_window_glade_file = "../glade/AboutWindow.xml"
argument_window_glade_file = "../glade/ArgumentWindow.xml"
viewer_window_glade_file = "../glade/ViewerWindow.xml"
add_section_window_glade_file = "../glade/AddSectionWindow.xml"


class MainWindow():
    """
    This class generates and handles events in the main window.
    It is the main working area where users input text into the
    various sections, add arguments to their command, save, load, etc.
    """

    # Quit application by Xing out the window, or File->Quit
    def on_main_window_destroy(self, widget, data=None):
        gtk.main_quit()

    # Open About window through Help->About
    def on_about_menu_item_activate(self, widget, data=None):
        AboutWindow()

    # Open Arguments window by clicking Arguments/Flags button
    def on_arguments_button_activate(self, widget, data=None):
        ArgumentWindow()

    # Open Viewer window by clicking View button
    def on_view_button_activate(self, widget, data=None):
        # Collect all text into a (dictionary?)
        ViewerWindow(); # Call with (dictionary?)

    # Open Add Section window by clicking Add Section button
    def on_add_section_button_activate(self, widget, data=None):
        AddSectionWindow()

    # Initialize main window
    def __init__(self):
        main_builder = gtk.Builder()
        main_builder.add_from_file(main_window_glade_file)

        self.main_window = main_builder.get_object("main_window")
        main_builder.connect_signals(self)


class AboutWindow():
    """
    This class generates and handles events in the About window.
    It displays information about the application.
    """

    def on_about_window_destroy(self, widget, data=None):
        self.about_window.hide()

    def __init__(self):
        about_builder = gtk.Builder()
        about_builder.add_from_file(about_window_glade_file)

        self.about_window = about_builder.get_object("about_window")
        about_builder.connect_signals(self)
        self.about_window.show()


class ArgumentWindow():
    """
    This class generates and handles events in the Arguments window.
    It allows users to add and delete arguments and flags.

    When a new argument/flag is added, this adds one row to the argument_list
    vbox (or removes one when an argument/flag is deleted.

    The arguments/flags are shown as selectable labels.
    """

    def on_argument_window_destroy(self, widget, data=None):
        self.argument_window.hide()

    def __init__(self):
        argument_builder = gtk.Builder()
        argument_builder.add_from_file(argument_window_glade_file)

        self.argument_window = argument_builder.get_object("argument_window")
        argument_builder.connect_signals(self)
        self.argument_window.show()


class ViewerWindow():
    """
    This class generates and handles events for the Viewer window.
    The Viewer allows the user to view man pages in a formatted,
    colorful environment.
    """

    def on_viewer_window_destroy(self, widget, data=None):
        self.viewer_window.hide()

    def __init__(self):
        viewer_builder = gtk.Builder()
        viewer_builder.add_from_file(viewer_window_glade_file)

        self.viewer_window = viewer_builder.get_object("viewer_window")
        viewer_builder.connect_signals(self)
        self.viewer_window.show()


class AddSectionWindow():
    """
    This class generates and handles events for the Add Section window.
    This allows the user to personalize their man page beyond what the
    program initially proposes by adding text sections.
    """

    def __init__(self):
        section_builder = gtk.Builder()
        section_builder.add_from_file(add_section_window_glade_file)

        self.section_window = section_builder.get_object("add_section_window")
        section_builder.connect_signals(self)
        self.section_window.show()


# Run application
if __name__ == "__main__":
    mpc = MainWindow()
    mpc.main_window.show()
    gtk.main()

